package com.learn.ccs;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {

    public static final int NUM = 7;

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        //集齐7颗龙珠,召唤神龙!
        CyclicBarrier cyc = new CyclicBarrier(NUM,()-> System.out.println("召唤神龙"));
        for (int i = 1; i <= NUM; i++) {
            final int temp = i;
            new Thread(()->{
                System.out.println("收集到第"+temp+"颗龙珠!");
                try {
                    cyc.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },temp+"").start();
        }
    }
}
